package com.jackeysun.app_common

import android.content.Context
import com.jackeysun.app_common.http.ApiServeImpl
import com.jackeysun.common.text.IViewProtocol
import com.jackeysun.common.text.AbsPopupWindow
import com.jackeysun.common.text.SimplePopupWindow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class CustomPopWindow(context: Context) : SimplePopupWindow(context),
    CoroutineScope by MainScope() {

    override fun showAtLocation(anchor: IViewProtocol, gravity: Int, x: Int, y: Int) {
        super.showAtLocation(anchor, gravity, x, y)

        launch {
            val words = ApiServeImpl.translate(anchor.getSelectedText().toString())
            if (words?.errorCode == 0) {
                mBinding.tvContent.text = words.translation.toString()
            }
        }
    }
}